package com.qeegoo.b2bautozityp.moudle.login.view;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.ActivityPhoneValidateBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerPhoneValidateComponent;
import com.qeegoo.b2bautozityp.moudle.base.AppBar;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.login.viewmodel.PhoneValidateViewModel;

import javax.inject.Inject;

/**
 * Created by lining on 2017/9/19.
 */

public class PhoneValidateActivity extends BaseActivity<ActivityPhoneValidateBinding> implements OnClickListener {

    @Inject
    PhoneValidateViewModel mViewModel;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_phone_validate;
    }

    @Override
    protected void initInjector() {
        DaggerPhoneValidateComponent.builder().appComponent(getAppComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        initToolBar(mBinding.layoutToolbar, new AppBar("手机验证码", true));
        mBinding.setViewModel(mViewModel);
        mBinding.timeButtonCode.setEnabled(false);
        mBinding.timeButtonCode.setTextAfter("秒后重发").setTextBefore("获取验证码").setLenght(59 * 1000);
        mBinding.timeButtonCode.setOnClickListener(this);
        mBinding.editTextPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().length() > 0) {
                    mBinding.timeButtonCode.setEnabled(true);
                } else {
                    mBinding.timeButtonCode.setEnabled(false);
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.timeButton_code:{
                mViewModel.loadPhoneCode();
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mBinding.timeButtonCode.onDestroy();
        super.onDestroy();
    }
}
